package mbg;

import org.mybatis.generator.config.*;

/**
 * @Description
 * @Author luyanpu
 * @Date 2023/6/26
 */
public class GeneratorConfig {

    public static Configuration getGeneratorConfig() {

        // 实体类不生成XXXKey文件
        Context context = new Context(ModelType.FLAT);

        context.setId("mysql");

        context.setTargetRuntime("MyBatis3");

        /*添加属性*/
        context.addProperty("javaFileEncoding", "UTF-8");

        /*插件配置，这个是我自己的插件，没有自定义插件的同学可以不配这一节，删除即可*/

        PluginConfiguration pluginConfig = new PluginConfiguration();

        pluginConfig.setConfigurationType("mbg.MyBatisPlugin");
        pluginConfig.addProperty("author","lyp");

        context.addPluginConfiguration(pluginConfig);

        /*注释生成器配置*/
        CommentGeneratorConfiguration commentGeneratorConfig = new CommentGeneratorConfiguration();
        // 去掉自动生成的注释 因为不可用
        commentGeneratorConfig.addProperty("suppressAllComments", "true");
        commentGeneratorConfig.addProperty("suppressDate", "true");

        context.setCommentGeneratorConfiguration(commentGeneratorConfig);

        /*JDBC连接信息配置*/

        JDBCConnectionConfiguration jdbcConnectionConfig = new JDBCConnectionConfiguration();

        jdbcConnectionConfig.setDriverClass("com.mysql.cj.jdbc.Driver");

        //注意代码配置中JDBC连接字符串中的参数分隔符不需要再像xml配置文件中那样使用转义符
        jdbcConnectionConfig.setConnectionURL("jdbc:mysql://XXXX/XXXX?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true");

        jdbcConnectionConfig.setUserId("root");

        jdbcConnectionConfig.setPassword("Root@xy-ai1");

        jdbcConnectionConfig.addProperty("nullCatalogMeansCurUPTyhJJrent", "true");//MySQL无法识别table标签中schema类的配置，所以在URL上指明目标数据库，并追加nullCatalogMeansCurrent属性为true

        jdbcConnectionConfig.addProperty("remarksReporting", "true");//针对oracle数据库无法读取表和字段备注

        jdbcConnectionConfig.addProperty("useInformationSchema", "true");//针对mysql数据库无法读取表和字段备注

        context.setJdbcConnectionConfiguration(jdbcConnectionConfig);

        /*Model生成器配置*/

        JavaModelGeneratorConfiguration javaModelGeneratorConfig = new JavaModelGeneratorConfiguration();

        javaModelGeneratorConfig.setTargetProject("./src/main/java/output");//目标项目(源码主路径)

        javaModelGeneratorConfig.setTargetPackage("com.bot.entity");//目标包(Model类文件存放包)
        javaModelGeneratorConfig.addProperty("enableSubPackages","false");//目标包(Model类文件存放包)
        // 去掉空格

        javaModelGeneratorConfig.addProperty("trimStrings","true");

        context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfig);

        /*SqlMapper生成器配置(*Mapper.xml类文件)，要javaClient生成器类型配合*/

        SqlMapGeneratorConfiguration sqlMapGeneratorConfig = new SqlMapGeneratorConfiguration();

        sqlMapGeneratorConfig.setTargetProject("./src/main/java/output/resources");//目标项目(源码主路径)

        sqlMapGeneratorConfig.setTargetPackage("mapper");//getPackage("com.xgclassroom.mapper");//目标包(*Mapper.xml类文件存放包)

        sqlMapGeneratorConfig.addProperty("enableSubPackages","false");
        context.setSqlMapGeneratorConfiguration(sqlMapGeneratorConfig);

        /*JavaClient生成器配置(*Mapper.java类文件)*/

        JavaClientGeneratorConfiguration javaClientGeneratorConfig = new JavaClientGeneratorConfiguration();

        javaClientGeneratorConfig.setConfigurationType("XMLMAPPER");//JavaClient生成器类型(主要有ANNOTATEDMAPPER、MIXEDMAPPER、XMLMAPPER，要Context的TargetRuntime配合)

        javaClientGeneratorConfig.setTargetProject("./src/main/java/output");//目标项目(源码主路径)

        javaClientGeneratorConfig.setTargetPackage("com.bot.mapper");//目标包(*Mapper.java类文件存放包)
        javaClientGeneratorConfig.addProperty("enableSubPackages","false");
        context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfig);

        /*表生成配置*/

        TableConfiguration tableConfig = new TableConfiguration(context);
        // 生成全部表
        tableConfig.setTableName("%");

        tableConfig.setCountByExampleStatementEnabled(false);
        tableConfig.setDeleteByExampleStatementEnabled(false);
        tableConfig.setUpdateByExampleStatementEnabled(false);
        tableConfig.setSelectByExampleStatementEnabled(false);
//        GeneratedKey generatedKey = new GeneratedKey("id", "JDBC", true, null);//设置主键列和生成方式
//
//        tableConfig.setGeneratedKey(generatedKey);

        context.addTableConfiguration(tableConfig);

        Configuration config = new Configuration();
        config.addContext(context);
        return config;

    }
}
